Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@csstools/css-tokenizer
Advanced tools
@csstools/css-tokenizer is a library for tokenizing CSS. It breaks down CSS code into a stream of tokens, which can be useful for building CSS parsers, linters, or other tools that need to analyze or manipulate CSS code.
Tokenizing CSS
This feature allows you to tokenize a CSS string. The `tokenize` function takes a CSS string and returns an iterable of tokens, which can then be processed or analyzed.
const { tokenize } = require('@csstools/css-tokenizer');
const css = 'body { color: red; }';
const tokens = tokenize(css);
for (const token of tokens) {
console.log(token);
}
Handling different token types
This feature demonstrates how to handle different types of tokens. The tokens returned by the `tokenize` function are arrays where the first element is the type of token and the second element is the value. This allows you to differentiate between different token types such as words, whitespace, etc.
const { tokenize } = require('@csstools/css-tokenizer');
const css = 'body { color: red; }';
const tokens = tokenize(css);
for (const token of tokens) {
if (token[0] === 'word') {
console.log('Word token:', token[1]);
} else if (token[0] === 'whitespace') {
console.log('Whitespace token');
}
}
PostCSS is a tool for transforming CSS with JavaScript plugins. It includes a tokenizer and parser, making it a more comprehensive tool compared to @csstools/css-tokenizer, which focuses solely on tokenization.
CSSTree is a toolset for CSS including a fast detailed parser, walker, generator, and lexer. It provides more advanced features like AST generation and manipulation, whereas @csstools/css-tokenizer focuses on tokenization.
The 'css' package is a CSS parser and stringifier. It provides a full parser that converts CSS into an AST and back, offering more functionality than just tokenization, which is the primary focus of @csstools/css-tokenizer.
Implemented from : https://www.w3.org/TR/2021/CRD-css-syntax-3-20211224/
Add CSS Tokenizer to your project:
npm install @csstools/css-tokenizer --save-dev
import { tokenizer, TokenType } from '@csstools/css-tokenizer';
const myCSS = `@media only screen and (min-width: 768rem) {
.foo {
content: 'Some content!' !important;
}
}
`;
const t = tokenizer({
css: myCSS,
});
while (true) {
const token = t.nextToken();
if (token[0] === TokenType.EOF) {
break;
}
console.log(token);
}
Or use the tokenize
helper function:
import { tokenize } from '@csstools/css-tokenizer';
const myCSS = `@media only screen and (min-width: 768rem) {
.foo {
content: 'Some content!' !important;
}
}
`;
const tokens = tokenize({
css: myCSS,
});
console.log(tokens);
{
onParseError?: (error: ParseError) => void
}
onParseError
The tokenizer is forgiving and won't stop when a parse error is encountered.
To receive parsing error information you can set a callback.
import { tokenizer, TokenType } from '@csstools/css-tokenizer';
const t = tokenizer({
css: '\\',
}, { onParseError: (err) => console.warn(err) });
while (true) {
const token = t.nextToken();
if (token[0] === TokenType.EOF) {
break;
}
}
Parser errors will try to inform you where in the tokenizer logic the error happened. This tells you what kind of error occurred.
Things this package aims to be:
What it is not:
FAQs
Tokenize CSS
The npm package @csstools/css-tokenizer receives a total of 3,401,599 weekly downloads. As such, @csstools/css-tokenizer popularity was classified as popular.
We found that @csstools/css-tokenizer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.